import 'package:flutter/material.dart';
import 'utils.dart';

abstract class BaseLayout extends StatefulWidget {
  //相对于父容器的位置
  final AlignmentGeometry alignment;
  //部局宽度
  final double width;
  //部局高度
  final double height;
  //包裹内边距
  final EdgeInsetsGeometry padding;
  //包裹外边距
  final EdgeInsetsGeometry margin;
  //是否可见
  final bool isVisible;
  //包裹修饰
  final BoxDecoration decoration;
  BaseLayout({
    Key key,
    this.alignment,
    this.width,
    this.height,
    this.decoration,
    this.margin,
    this.padding,
    this.isVisible,
  }) : super(key: key);
  @override
  State createState();
  @override
  StatefulElement createElement() {
    //print("widget ${this} element created!");
    return super.createElement();
  }
}

abstract class BaseState {
  bool visible = true;
  BaseLayout baseLayout;

  Widget buildState(Function function) {
    visible=baseLayout.isVisible;
    return isNotNull(visible)
        ? Offstage(
            offstage: !visible,
            child: getContent(function),
          )
        : getContent(function);
  }
  Widget getContent(Function function){
    if (isNeedContainerWidget())
      return wrapContainer(function());
    else
      return function();
  }
  Widget wrapContainer(Widget child) {
    return Container(
      height: baseLayout.height,
      width: baseLayout.width,
      decoration: baseLayout.decoration,
      padding: baseLayout.padding,
      margin: baseLayout.margin,
      alignment: baseLayout.alignment,
      child: child,
    );
  }
  bool isNeedContainerWidget() {
    return isNotNull(baseLayout.decoration) ||
        isNotNull(baseLayout.padding) ||
        isNotNull(baseLayout.margin) ||
        isNotNull(baseLayout.width) ||
        isNotNull(baseLayout.height);
  }

  bool isVisible() {
    return visible;
  }
  Widget buildContent();
}
